﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZeroBug.EasyFlow.Core;

namespace ZeroBug.EasyFlow.Step
{
    public class WaitStatusStep : IStep
    {
        public bool IsSingleRun { get; set; }
        public bool IgnoreChildren { get; set; }
        public string Icon { get; set; } = "TimeSlot";
        public int ChildrenIndex { get; set; }
        public Core.Step? NextStep { get; set; }

        VariableExpression waitVariable;
        VariableExpression waitValue;
        VariableExpression timeOut;
        VariableExpression alarmInfo;
        public bool Compile(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            if (!step.Parameters.ContainsKey("WaitVariable"))
            {
                errInfo = "不存在WaitVariable变量";
                return false;
            }
            waitVariable = step.Parameters["WaitVariable"];

            if (!step.Parameters.ContainsKey("WaitValue"))
            {
                errInfo = "不存在WaitValue变量";
                return false;
            }
            waitValue = step.Parameters["WaitValue"];

            if (!step.Parameters.ContainsKey("TimeOut"))
            {
                errInfo = "不存在TimeOut变量";
                return false;
            }
            timeOut = step.Parameters["TimeOut"];

            return true;
        }

        public object GetForm()
        {
            throw new NotImplementedException();
        }

        public StepInfo GetInfo()
        {
            StepInfo info = new StepInfo();
            info.StepName = "WaitStatus";
            info.Group = StepGroup.Variable;
            info.ImagePath = "pack://application:,,,/ZeroBug.EasyFlow.Step;component/Images/条件.png";
            return info;
        }

        public void OnChildrenEnter()
        {
            throw new NotImplementedException();
        }

        public void OnChildrenLeave()
        {
            throw new NotImplementedException();
        }

        public bool Prepare(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            return true;
        }

        public void RegisterVariables(FlowBase flow, Core.Step step)
        {

        }

        public StepResult Run(FlowBase flow, Core.Step step, out string errInfo)
        {
            errInfo = string.Empty;

            DateTime startTime = DateTime.Now;
            TimeSpan ts = DateTime.Now - startTime;
            while (!waitVariable.GetEquivalent()[0].ToLower().Equals(waitValue.GetEquivalent()[0].ToLower()) &&
                   ts.TotalMilliseconds < Convert.ToInt32(timeOut.GetEquivalent()[0]))
            {
                ts = DateTime.Now - startTime;
                Thread.Sleep(10);
            }
            if (ts.TotalMilliseconds >= Convert.ToInt32(timeOut.GetEquivalent()[0]))
            {
                errInfo = alarmInfo.GetEquivalent()[0];
                IgnoreChildren = false;
            }
            IgnoreChildren = true;
            return StepResult.Success;
        }

        public string GetTip(Core.Step step)
        {
            return "";
        }

        public (SerializableDictionary<string, VariableExpression>, List<VariableJudge>) GetParametersAndConditions()
        {
            throw new NotImplementedException();
        }

        public void UnRegisterVariables(FlowBase flow, Core.Step step)
        {
        }
    }
}
